package com.kyzh.core.pager.weal.community

import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.ContextCompat
import androidx.core.util.Pair
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.atools.util.AView
import com.gushenge.core.beans.CommunityBean
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.GlobalKeys
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.FragCommunityBean
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.pager.weal.community.viewmodel.CommunityViewModel
import com.kyzh.core.databinding.ActCommuinityBinding
import com.kyzh.core.databinding.FloatViewBinding
import com.kyzh.core.databinding.ItemCommunityNewsBinding
import com.kyzh.core.dialog.showCommunityListDialog
import com.kyzh.core.utils.ViewUtils
import com.kyzh.core.utils.initCommunity
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startActivity
import per.goweii.layer.overlay.OverlayLayer
import per.goweii.layer.overlay.ktx.*

class CommunityActivity: BaseActivity() {
    private lateinit var db : ActCommuinityBinding
    private var id =""
    private val vm by viewModels<CommunityViewModel>()
    private val newsAdapter = NewsAdapter()
    private var isInitTab = false
    private lateinit var floatView : FloatViewBinding
    companion object{
        @JvmStatic
        fun start(context: Activity,id:String,vararg a: Pair<View, String>) {
            val starter = Intent(context, CommunityActivity::class.java)
                .putExtra("id",id)
            if(a == null|| a.isEmpty()){
                context.startActivity(starter)
            }else{
                val bundle: Bundle? = ActivityOptionsCompat.makeSceneTransitionAnimation( context, *a ).toBundle()
                context.startActivity(starter,bundle)

            }

        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this, R.layout.act_commuinity)
        floatView = FloatViewBinding.inflate(layoutInflater)
        id =  intent.getStringExtra("id")
        AView.setMargins(db.toolbar,0, AView.getStatusBarHeight(this),0,0)
        initView()
        getData()


        db.revTop.adapter =newsAdapter
        OverlayLayer(this)
            .overlayView(floatView.root)
            .snapEdge(OverlayLayer.Edge.RIGHT)
            .normalAlpha(1F)
            .normalScale(1F)
            .defAlpha(0.95F)
            .defPercentX(1F)
            .defPercentY(0.618F)
            .lowProfileAlpha(0.6F)
            .lowProfileDelay(3000)
            .lowProfileIndent(0.2F)
            .lowProfileScale(0.9F)
            .show()
    }

    fun initView(){
        db.ivGuanzhu.setOnClickListener {
            db.ivGuanzhu.isEnabled = false
            vm.guanzhu(id){
                getData()
            }
        }
        floatView.apply {
            btAdd.setOnClickListener {
                CommunityPingLunActivity.start(this@CommunityActivity,id)
            }
            btDian.setOnClickListener {
                CommunityNewsActivity.start(this@CommunityActivity)
            }
        }
        db.close.setOnClickListener {
            finish()
        }
        db.tv1.setOnClickListener {
            showCommunityListDialog()
        }
        db.appbar.addOnOffsetChangedListener(object : ViewUtils.AppBarLayoutStateChangeListener() {
            override fun onStateChanged(appBarLayout: AppBarLayout, state: State) {
                when (state) {
                    State.EXPANDED -> {
                        AView.setStatusBar(this@CommunityActivity, false)
                        db. close.imageTintList =  ColorStateList.valueOf(Color.WHITE)
                        db.titleName.setVisibility(false)

                    }
                    State.COLLAPSED -> {
                        AView.setStatusBar(this@CommunityActivity, true)
                        db. close.imageTintList =  ColorStateList.valueOf(Color.BLACK)
                        db.titleName.setVisibility(true)
                    }
                    State.INTERMEDIATE -> {
                        AView.setStatusBar(this@CommunityActivity, false)
                        db. close.imageTintList =  ColorStateList.valueOf(Color.WHITE)
                        db.titleName.setVisibility(false)
                    }
                }
            }
        })


    }
    fun initTab(fenlei: ArrayList<CommunityBean.Data.Fenlei>,name:String){
        if(isInitTab){
            return
        }
        isInitTab = true
        val mediator  = TabLayoutMediator(db.tab, db. viewpager) { tab, position -> //这里可以自定义TabView
            tab.text = fenlei[position].name
        }
        db.viewpager.initCommunity(this,id,fenlei,name)
        mediator.attach()
    }

    fun getData(){
        vm.getCommunityTop(id){
            db.ivGuanzhu.isEnabled = true
            db.d = this
            newsAdapter.setNewInstance(this.news)
            if(this.name==null){
                this.name = ""
            }
            initTab(this.fenlei,this.name)
        }
    }

    private inner class NewsAdapter : BaseQuickAdapter<CommunityBean.Data.New,BaseDataBindingHolder<ItemCommunityNewsBinding>>(R.layout.item_community_news){
        override fun convert(
            holder: BaseDataBindingHolder<ItemCommunityNewsBinding>,
            item: CommunityBean.Data.New
        ) {
            holder.dataBinding?.apply {
                d = item
                when(holder.layoutPosition){
                    0->type.backgroundTintList =  ColorStateList.valueOf(ContextCompat.getColor(context,R.color.aAD52FF))
                    1->type.backgroundTintList =  ColorStateList.valueOf(ContextCompat.getColor(context,R.color.bq8))
                    2->type.backgroundTintList =  ColorStateList.valueOf(ContextCompat.getColor(context,R.color.aFA8C08))
                }
                root.setOnClickListener {
                    CommunityDetailActivity.start(context,item.id,item.title)
//                    startActivity<BrowserActivity>(GlobalKeys.TITLE to item.title, GlobalKeys.LINK to GlobalConsts.NEWS_XQ + item.id)
                }
            }
        }
    }
}